<form id="templateForm" class="form-horizontal"  method="post">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h5 class="blue">新增模板</h5>
    </div>

    <div class="modal-body">
        <!--显示父类 -->
        <input type="hidden" name="id" value="${template.id!}">
        <div class="form-group">
            <label class="control-label col-xs-12 col-sm-2 no-padding-right" for="templateName">模板名:</label>
            <div class="col-xs-12 col-sm-9">
                <div class="clearfix">
                    <input type="text" name="templateName" id="templateName" class="col-xs-12 col-sm-4 required" value="${template.templateName!}"/>
                </div>
            </div>
        </div>

        <div class="form-group">
            <label class="control-label col-xs-12 col-sm-2 no-padding-right" for="templateType">模板类型:</label>
            <div class="col-xs-5 col-sm-9">
                <div class="clearfix">
                    <div>
                        <select class="chosen-select form-control required templateType" name="templateType" id="templateType">
                            <#list fieldItems as item>
                            <option value="${item.id!}" <#if template.templateType?? && item.id==template.templateType>selected </#if>>${item.itemTitle!}</option>
                            </#list>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-xs-8 col-sm-2 no-padding-right" for="templateNote">模板内容:</label>
            <div class="col-xs-20 col-sm-15">
                <div class="clearfix">
                    <span class="col-xs-20 col-sm-15 input-icon input-icon-right">
                        <textarea rows="15" cols="100"  name="templateNote" id="templateNote" value="${template.templateNote!}" class="col-xs-20 col-sm-15 required templateNote">${template.templateNote!}</textarea>
                    </span>
                </div>
            </div>
        </div>
        <!--上传ICON块---->
    <div class="modal-footer center">
        <button type="submit" class="btn btn-sm btn-success"><i class="ace-icon fa fa-check"></i> 保存</button>
        <button type="button" class="btn btn-sm" data-dismiss="modal"><i class="ace-icon fa fa-times"></i> 取消</button>
    </div>

</form>
<script src="${ctx}/static/admin/assets/js/jquery.validate.js"></script>
<script src="${ctx}/static/admin/assets/js/jquery.validate.messages_zh.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $(".date-picker").datepicker({
            language : 'zh-CN',
            autoclose: true,
            todayHighlight: true
        });

        $("#templateForm").validate({
            errorElement: 'div',
            errorClass: 'help-block',
            focusInvalid: true,
            highlight: function (e) {
                $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
            },
            success: function (e) {
                $(e).closest('.form-group').removeClass('has-error');//.addClass('has-info');
                $(e).remove();
            },
            errorPlacement: function (error, element) {
                if(element.is('input[type=checkbox]') || element.is('input[type=radio]')) {
                    var controls = element.closest('div[class*="col-"]');
                    if(controls.find(':checkbox,:radio').length > 1) controls.append(error);
                    else error.insertAfter(element.nextAll('.lbl:eq(0)').eq(0));
                }
                else if(element.is('.select2')) {
                    error.insertAfter(element.siblings('[class*="select2-container"]:eq(0)'));
                }
                else if(element.is('.chosen-select')) {
                    error.insertAfter(element.siblings('[class*="chosen-container"]:eq(0)'));
                }
                else error.insertAfter(element.parent());
            },

            submitHandler: function (form) {
                $.ajax({
                    type: "POST",
                    url:"${ctx}/admin/template/save.do",
                    data:$("#templateForm").serialize(),
                    dataType: 'text',
                    success: function(msg){
                        var msg = eval("("+msg+")");
                        if(msg.result == "success"){
                            window.location="${refer!}";
                        }else{
                            alert(msg.messages);
                        }
                    },
                    error: function(){
                        bootbox.alert("保存失败");
                    }
                });
            },
            invalidHandler: function (form) {
            }
        })

    });
</script>